package com.cxrj.zhsq.userManage.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cxrj.zhsq.annotation.SystemControllerLog;
import com.cxrj.zhsq.userManage.entity.Organization;
import com.cxrj.zhsq.userManage.service.OrganizationService;

/**
 * @Author: WangBo
 * @Description:
 * @Date: Create in 15:30 2017/11/29
 * @Modified By:
 */
@RestController
public class OrganizationController {

	@Autowired
	private OrganizationService organizationService;

	/**
	 * @Author: WangBo
	 * @Description: 新增组织
	 * @param map
	 *            参数map
	 * @Date: 15:55 2017/11/30
	 */
	@RequestMapping(value = "insertOrganization", method = RequestMethod.POST)
	@SystemControllerLog(description = "新增社区")
	public Map<String, Object> insertOrganization(Organization organization, HttpSession session) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = organizationService.insertOrganization(organization);
		} catch (Exception e) {
			resultMap.put("result", 0);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "updateOrganization", method = RequestMethod.POST)
	public Map<String, Object> updateOrganization(Organization organization, HttpSession session) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = organizationService.updateOrganization(organization);
		} catch (Exception e) {
			resultMap.put("result", 0);
		}
		return resultMap;
	}

	@RequestMapping(value = "selectOrganizationByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectOrganizationByPage(HttpServletRequest request, HttpServletResponse response) {
		return organizationService.selectOrganizationByPage(request, response);
	}

	@RequestMapping(value = "selectOrganizationById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectOrganizationById(Integer id, HttpServletRequest request) {
		return organizationService.selectOrganizationById(id);
	}

	/**
	 * 
	 * @author Hanxing
	 * @date 2018年3月1日 上午11:27:59
	 * @Description 查询所有社区列表 key ： list
	 */
	@RequestMapping(value = "selectAllCommunityList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectAllCommunityList() {
		return organizationService.selectAllCommunityList("false");
	}

	@RequestMapping(value = "setCommunity", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> setCommunity(String communityId, String communityName, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		request.getSession().setAttribute("communityName", communityName);
		request.getSession().setAttribute("communityId", communityId);
		return resultMap;
	}
}
